//
//  ViewController.swift
//  APSwift
//
//  Created by AbnerPei on 2021/9/20.
//

import UIKit
import APExtensions

class ViewController: UIViewController {
    
    private var apView: APView!
    
    lazy var tableView: UITableView = {
        let tableV = UITableView(frame: view.bounds, style: .plain)
        tableV.delegate = self
        tableV.dataSource = self
        tableV.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        return tableV
    }()
    
    var cellTitles = [String]()
    var cellVCClasses = [UIViewController.Type]()
    
    override func didMove(toParent parent: UIViewController?) {
        super.didMove(toParent: parent)
        print("执行了ViewController的didMove(toParent parent: \(String(describing: parent))")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        view.backgroundColor = .white
        self.title = "AbnerPei的Swift"
        
        debugPrint(" UIApplication.shared.delegate is \(String(describing: UIApplication.shared.delegate))")
        
        configDatas()
        view.addSubview(tableView)
        
        apView = APView(params: ["key": "你好"])
        
        printSth()
    }
    
    func configDatas() {
        cellTitles = [
            "SwiftUI",
            "Swift_入门",
            "Swfit_概念篇",
            "Swift_API",
            "Swift_算法",
            "【完善ing】标准化代码",
            "UIs示例",
            "APLibs的examples",
            "实验室",
            "底层原理探究",
            "开源库源码"
        ]
        
        cellVCClasses = [
            SwiftUI_DemoVC.self,
            Primer_TableVC.self,
            Concept_TableVC.self,
            API_TableVC.self,
            Algorithm_TableVC.self,
            StandardizedCode_TableVC.self,
            UIs_TableVC.self,
            APLibs_TableVC.self,
            Labs_TableVC.self,
            UnderlyingInquiry_TableVC.self,
            OpenSourceLibrary_TableVC.self
        ]
    }
    
}

extension ViewController: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return cellTitles.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//        print("\(#function)")
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = cellTitles[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//        print("\(#function)")
        return 60
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        pushToNext(vcTitles: cellTitles, vcClasses: cellVCClasses, at: indexPath)
    }
}

extension ViewController {

    private func printSth() {
        let statusHeight = ap_kStatusHeight
        debugPrint("123456 statusHeight is \(statusHeight)")
        
        decimalNumberTest()
    }
    
    private func decimalNumberTest() {
        let numStr1 = "5.555555"
        let numStr2 = "0.1111111"
        let sum = (numStr1 as NSString).doubleValue + (numStr2 as NSString).doubleValue
        debugPrint("888888 sum is \(sum)")

        let decimalNum1 = NSDecimalNumber(string: numStr1)
        let decimalNum2 = NSDecimalNumber(string: numStr2)
        let sum2 = "\(decimalNum1.adding(decimalNum2).doubleValue)"
        debugPrint("888888 sum2 is \(sum2)")
    }
}
